import React from 'react'
import Tit from '../../components/Tit'
import { withRouter } from 'react-router-dom'
import { InputItem } from 'antd-mobile'
import './AddCard.less'
class AddCard extends React.Component {
    state = {
        inviteCode: ''
    }
    handleChange(key, value) {
        console.log(key, value)
        this.setState({
            [key]: value
        })
    }
    GoCode() {
        if (this.state.inviteCode === '') { return }
        console.log(this.props.match.params.ID)
        this.props.match.params.ID==0?this.$postData('/api/order/validaCode', { inviteCode: this.state.inviteCode, type: this.props.match.params.ID, goodsId: this.props.match.params.goodsId, num: this.props.match.params.num }, params => {
            console.log(params)
            switch (params.code) {
                case 100:
                    this.props.history.push(`/FillOrder/0/${this.props.match.params.commonId}/${this.props.match.params.goodsId}/${this.props.match.params.num}/${this.props.match.params.addressId}/${this.state.inviteCode}`)
                    break;
                default:
                    this.Toast.info(params.msg);
                    break;
            }
        }):this.$postData('/api/order/validaCode', { inviteCode: this.state.inviteCode, type: this.props.match.params.ID, cartId: this.props.match.params.commonId }, params => {
            console.log(params)
            switch (params.code) {
                case 100:
                    this.props.history.push(`/FillOrder/1/${this.props.match.params.commonId}/${this.props.match.params.goodsId}/${this.props.match.params.num}/${this.props.match.params.addressId}/${this.state.inviteCode}`)
                    break;
                default:
                    this.Toast.info(params.msg);
                    break;
            }
        })
    }
    componentDidMount() {
        console.log(this.props)
    }
    render() {
        return (
            <div className='AddCard-d1'>
                <Tit tit={'填写推广码'} zz={true}></Tit>
                <p className='AddCard-p1'>填写推广可享受优惠</p>
                <InputItem
                    className='AddCard-d2'
                    placeholder="填写推广码"
                    value={this.state.inviteCode}
                    onChange={this.handleChange.bind(this, 'inviteCode')}
                ></InputItem>
                <div className='center AddCard-d3' onClick={this.GoCode.bind(this)}>确定</div>
            </div>
        )
    }
}

export default withRouter(AddCard)